package org.binglan.youziyuepai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.collections.map.LinkedMap;
import org.binglan.youziyuepai.entity.ActivityToOccupation;
import org.binglan.youziyuepai.mapper.ActivityToOccupationMapper;
import org.binglan.youziyuepai.service.ActivityToOccupationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.binglan.youziyuepai.service.OccupationService;
import org.binglan.youziyuepai.utils.EnumUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 活动职业多对多表 服务实现类
 * </p>
 *
 * @author sanjin
 * @since 2019-04-09
 */
@Service
public class ActivityToOccupationServiceImpl extends ServiceImpl<ActivityToOccupationMapper, ActivityToOccupation> implements ActivityToOccupationService {

    @Autowired
    OccupationService occupationService;

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Boolean saveActivityToOccupationBatch(List<ActivityToOccupation> activityToOccupationList) {

        return saveBatch(activityToOccupationList);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Map<String, Integer> queryByActivityId(Integer activityId) {
        Map<String,Integer> result = new HashMap<>();
        ActivityToOccupation queryActivityToOccupation = new ActivityToOccupation();
        queryActivityToOccupation.setActivityId(activityId);

        List<ActivityToOccupation> list = this.list(new QueryWrapper<>(queryActivityToOccupation));
        list.forEach(item -> {
            result.put(occupationService.queryNameById(item.getOccupationId()), item.getNum());
        });
        return result;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public List<ActivityToOccupation> queryActivityToOccupationsByActivityId(Integer activityId) {
        ActivityToOccupation queryActivityToOccupation = new ActivityToOccupation();
        queryActivityToOccupation.setActivityId(activityId);

        return this.list(new QueryWrapper<>(queryActivityToOccupation));
    }
}
